<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title><?php echo $this->siteConfig['title']; ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/static/default/css/style.css">
</head>
</head>
<body>
<div class="frame" id="frame">
  <header class="titlebar">
    <div class="dot" title="Close"   onclick="location.reload()"></div>
    <div class="dot" title="Minimize" onclick="minimize()"></div>
    <div class="dot" title="Fullscreen" onclick="toggleFullscreen()"></div>
  </header>

  <main class="content">
    <?php
    // 加载配置文件
    $siteConfig = $this->siteConfig;
    $blogList = $this->blogList;
    $partitions = $siteConfig['partitions'] ?? [];
    ?>

    <!-- About -->
    <h1>// About</h1>
    <p><?php echo $siteConfig['about']; ?></p>

    <!-- 社交媒体模块 -->
    <div class="social-media">
        <h2>// Social Media</h2>
        <div class="social-links">
            <?php 
            if(isset($siteConfig['platforms']) && is_array($siteConfig['platforms'])): 
                foreach($siteConfig['platforms'] as $platform):
                    if($platform['value'] !== '#'): 
            ?>
            <div class="social-item">
                <span class="social-label"><?php echo $platform['name']; ?>:</span>
                <?php if($platform['type'] === 'url'): ?>
                    <a href="<?php echo htmlspecialchars($platform['value']); ?>" target="_blank"><?php echo htmlspecialchars($platform['value']); ?></a>
                <?php else: ?>
                    <span><?php echo htmlspecialchars($platform['value']); ?></span>
                <?php endif; ?>
            </div>
            <?php endif; endforeach; 
            endif; ?>
        </div>
    </div>
    <!-- Works -->
    <h2>// Works</h2>
    <div class="works" style="display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:16px;">
      <?php if(isset($siteConfig['works']) && is_array($siteConfig['works'])): 
          foreach ($siteConfig['works'] as $work): ?>
      <div class="card" style="padding:16px;">
        <h3><?php echo $work['title']; ?></h3>
        <p><?php echo $work['description']; ?></p>
        <a href="<?php echo $work['url']; ?>" target="_blank" style="display:inline-block;margin-top:8px;">查看项目 →</a>
        <?php if ($work['image'] !== '#'): ?>
        <img src="<?php echo $work['image']; ?>" alt="<?php echo $work['title']; ?>" style="width:100%;margin-top:12px;border-radius:6px;">
        <?php endif; ?>
      </div>
      <?php endforeach; 
      endif; ?>
    </div>

    <!-- Partitions -->
    <h2>// Partitions</h2>
    <div class="partitions" style="display:flex;gap:12px;margin-bottom:20px;">
      <?php foreach ($partitions as $partition): ?>
        <a href="/Partitions/<?php echo $partition['id']; ?>.html" style="padding:6px 12px;background:var(--glass);border-radius:6px;cursor:pointer;text-decoration:none;color:var(--text);" class="partition-btn"><?php echo $partition['title']; ?></a>
      <?php endforeach; ?>
    </div>

    <!-- Blog Posts -->
    <h2>// Latest Posts</h2>
    <div class="posts-container">
      <?php foreach ($partitions as $partition): ?>
        <div class="partition-latest-posts" style="margin-bottom:30px;">
          <h3 style="color:var(--kw);margin-bottom:12px;"><?php echo $partition['title']; ?></h3>
          <?php 
          // 获取该分区的所有帖子
          $partitionPosts = array_filter($blogList, function($post) use ($partition) {
              return $post['partition'] == $partition['id'];
          });
          // 先按置顶状态排序，再按时间降序排序
          usort($partitionPosts, function($a, $b) {
              $topA = isset($a['top']) && $a['top'] === true ? 1 : 0;
              $topB = isset($b['top']) && $b['top'] === true ? 1 : 0;
              
              if ($topA !== $topB) {
                  return $topB - $topA; // 置顶的排在前面
              }
              
              return strtotime($b['time']) - strtotime($a['time']); // 然后按时间降序
          });
          // 取前三条
          $latestPosts = array_slice($partitionPosts, 0, 3);
          ?>
          <?php if (count($latestPosts) > 0): ?>
            <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:16px;">
              <?php foreach ($latestPosts as $post): ?>
                <div class="card" style="margin-bottom:0;">
                  <div style="display:flex;justify-content:space-between;align-items:center;">
                    <h3><a href="/posts/<?php echo $post['id']; ?>.html"><?php echo $post['title']; ?></a></h3>
                    <?php if (isset($post['top']) && $post['top'] === true): ?>
                      <span style="background:var(--accent);color:white;padding:2px 8px;border-radius:4px;font-size:0.7em;">置顶</span>
                    <?php endif; ?>
                  </div>
                  <p><?php echo $post['author']; ?> | <?php echo $post['time']; ?></p>
                  <div style="margin-top:8px;">
                    <?php foreach ($post['tags'] as $tag): ?>
                      <span style="font-size:0.8em;background:rgba(255,255,255,0.1);padding:2px 8px;border-radius:12px;margin-right:4px;"><?php echo $tag; ?></span>
                    <?php endforeach; ?>
                  </div>
                </div>
              <?php endforeach; ?>
            </div>
            <div style="margin-top:10px;">
              <a href="/Partitions/<?php echo $partition['id']; ?>.html" style="color:var(--kw);">查看更多 →</a>
            </div>
          <?php else: ?>
            <p style="color:#d1d1d6;">该分区暂无帖子</p>
          <?php endif; ?>
        </div>
      <?php endforeach; ?>
    </div>
  </main>
</div>

<script src="/static/default/js/script.js"></script>
</body>
</html>